Sensor Fusion for Kinetis MCUs (ISSDK/KSDK version)
driver_MAG3110.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, NXP Semiconductor
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  * of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  * list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from this
17  * software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 /*! \file driver_MAG3110.c
31  \brief Provides init() and read() functions for the MAG3110 magnetometer.
32 */
33 
34 #include "board.h" // generated by Kinetis Expert. Long term - merge sensor_board.h into this file
35 #include "sensor_fusion.h" // Sensor fusion structures and types
36 #include "sensor_drv.h"
37 #include "sensor_io_i2c.h" // Required for registerreadlist_t / registerwritelist_t declarations
38 #include "drivers.h" // Device specific drivers supplied by NXP (can be replaced with user drivers)
39 #include "mag3110.h"
40 #define MAG3110_COUNTSPERUT 10 // fixed range for MAG3110 magnetometer
41 
42 #if F_USING_MAG
43 
44 // Command definition to read the WHO_AM_I value.
45 const registerreadlist_t MAG3110_WHO_AM_I_READ[] =
46 {
47  { .readFrom = MAG3110_WHO_AM_I, .numBytes = 1 }, __END_READ_DATA__
48 };
49 
50 // Command definition to read the number of entries in the accel FIFO.
51 const registerreadlist_t MAG3110_DR_STATUS_READ[] =
52 {
53  { .readFrom = MAG3110_DR_STATUS, .numBytes = 1 }, __END_READ_DATA__
54 };
55 
56 // Command definition to read the converted result
57 registerreadlist_t MAG3110_DATA_READ[] =
58 {
59  { .readFrom = MAG3110_OUT_X_MSB, .numBytes = 6 }, __END_READ_DATA__
60 };
61 
62 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
63 const registerwritelist_t MAG3110_Initialization[] =
64 {
65  // write 0000 0000 = 0x00 to CTRL_REG1 to place MAG3110 into standby
66  // [7-1] = 0000 000
67  // [0]: AC=0 for standby
68  { MAG3110_CTRL_REG1, 0x00, 0x00 },
69 
70  // write 1001 0000 = 0x90 to CTRL_REG2
71  // [7]: AUTO_MRST_EN=1: enable degaussing
72  // [6]: unused=0
73  // [5]: RAW=0: normal mode
74  // [4]: Mag_RST=1: enable a single degauss
75  // [3-0]: unused=0
76  { MAG3110_CTRL_REG2, 0x90, 0x00 },
77 
78  // write 000X X001 to CTRL_REG1 to set ODR and take MAG3110 out of standby
79  // [7-5]: DR=000 for 1280Hz ADC (to give best noise performance)
80  // [4-3]: OS=11 for 10Hz ODR giving 0x19
81  // [4-3]: OS=10 for 20Hz ODR giving 0x11
82  // [4-3]: OS=01 for 40Hz ODR giving 0x09
83  // [4-3]: OS=00 for 80Hz ODR giving 0x01
84  // [2]: FT=0 for normal reads
85  // [1]: TM=0 to not trigger immediate measurement
86  // [0]: AC=1 for active mode
87 #if (MAG_ODR_HZ <= 10) // select 10Hz ODR
88  { MAG3110_CTRL_REG1, 0x19, 0x00 },
89 #elif (MAG_ODR_HZ <= 30) // select 20Hz ODR (to give lower noise with standard 25Hz build)
90  { MAG3110_CTRL_REG1, 0x11, 0x00 },
91 #elif (MAG_ODR_HZ <= 40) // select 40Hz ODR
92  { MAG3110_CTRL_REG1, 0x09, 0x00 },
93 #else // select 80Hz ODR
94  { MAG3110_CTRL_REG1, 0x01, 0x00 },
95 #endif
96  __END_WRITE_DATA__
97 };
98 
99 // All sensor drivers and initialization functions have the same prototype.
100 // sfg is a pointer to the master "global" sensor fusion structure.
101 // sensor = pointer to linked list element used by the sensor fusion subsystem to specify required sensors
102 #pragma optimize=no_scheduling
104 {
105  int32_t status;
106  uint8_t reg;
107  status = Register_I2C_Read(sensor->bus_driver, sensor->addr, MAG3110_WHO_AM_I, 1, &reg);
108  if (status==SENSOR_ERROR_NONE) {
109  sfg->Mag.iWhoAmI = reg;
110  if (reg!=MAG3110_WHOAMI_VALUE) {
111  return(SENSOR_ERROR_INIT);
112  }
113  } else {
114  // iWhoAmI will return default value of zero
115  // return with error
116  return(SENSOR_ERROR_INIT);
117  }
118 
119  // Configure and start the MAG3110 sensor. This does multiple register writes
120  // (see MAG3110_Initialization definition above)
121  status = Sensor_I2C_Write(sensor->bus_driver, sensor->addr, MAG3110_Initialization );
122 
123  // Stash some needed constants in the SF data structure for this sensor
125  sfg->Mag.fCountsPeruT = (float)MAG3110_COUNTSPERUT; // IAR optimized this out without the #pragma before the function
126  sfg->Mag.fuTPerCount = 1.0F / MAG3110_COUNTSPERUT; // IAR optimized this out without the #pragma before the function
127 
128  sensor->isInitialized = F_USING_MAG; // IAR optimized this out without the #pragma before the function
129  sfg->Mag.isEnabled = true; // IAR optimized this out without the #pragma before the function
130 
131  return (status);
132 }
133 
135 {
136  uint8_t I2C_Buffer[6]; // I2C read buffer
137  int8_t status; // I2C transaction status
138  int16_t sample[3]; // Reconstructed sample
139 
140  if(sensor->isInitialized != F_USING_MAG)
141  {
142  return SENSOR_ERROR_INIT;
143  }
144 
145  status = Sensor_I2C_Read(sensor->bus_driver, sensor->addr, MAG3110_DATA_READ, I2C_Buffer );
146  sample[CHX] = (I2C_Buffer[0] << 8) | I2C_Buffer[1];
147  sample[CHY] = (I2C_Buffer[2] << 8) | I2C_Buffer[3];
148  sample[CHZ] = (I2C_Buffer[4] << 8) | I2C_Buffer[5];
149  if (status==SENSOR_ERROR_NONE) {
150  conditionSample(sample); // truncate negative values to -32767
151  sample[CHZ] = -sample[CHZ]; // +Z should point up (MAG3110 Z positive is down)
152  addToFifo((FifoSensor*) &(sfg->Mag), MAG_FIFO_SIZE, sample);
153  }
154 
155  return (status);
156 }
157 
158 
159 // Each entry in a RegisterWriteList is composed of: register address, value to write, bit-mask to apply to write (0 enables)
160 const registerwritelist_t MAG3110_IDLE[] =
161 {
162  // Set ACTIVE = 0
163  { MAG3110_CTRL_REG1, 0x00, 0x00 },
164  __END_WRITE_DATA__
165 };
166 
167 // MAG3110_Idle places the sensor into STANDBY mode (wakeup time = 25ms at ODR = 80Hz)
169 {
170  int32_t status;
171  if(sensor->isInitialized == F_USING_MAG) {
172  status = Sensor_I2C_Write(sensor->bus_driver, sensor->addr, MAG3110_IDLE );
173  sensor->isInitialized = 0;
174  sfg->Mag.isEnabled = false;
175  } else {
176  return SENSOR_ERROR_INIT;
177  }
178  return status;
179 }
180 
181 #endif
const registerreadlist_t MAG3110_DR_STATUS_READ[]
const registerreadlist_t MAG3110_WHO_AM_I_READ[]
#define CHY
Used to access Y-channel entries in various data data structures.
Definition: sensor_fusion.h:77
void addToFifo(FifoSensor *sensor, uint16_t maxFifoSize, int16_t sample[3])
addToFifo is called from within sensor driver read functions
The top level fusion structure.
void * bus_driver
should be of type (ARM_DRIVER_I2C* for I2C-based sensors, ARM_DRIVER_SPI* for SPI) ...
#define MAG_FIFO_SIZE
FXOS8700 (mag), MAG3110 have no FIFO so equivalent to 1 element FIFO.
int16_t iCountsPeruT
counts per uT
MagSensor Mag
magnetometer storage
The FifoSensor union allows us to use common pointers for Accel, Mag & Gyro logical sensor structures...
const registerwritelist_t MAG3110_IDLE[]
float fuTPerCount
uT per count
bool isEnabled
true if the device is sampling
The sensor_fusion.h file implements the top level programming interface.
int8_t MAG3110_Read(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
#define CHZ
Provides function prototypes for driver level interfaces.
#define CHX
Used to access X-channel entries in various data data structures.
Definition: sensor_fusion.h:76
void conditionSample(int16_t sample[3])
conditionSample ensures that we never encounter the maximum negative two&#39;s complement value for a 16-...
float fCountsPeruT
counts per uT
An instance of PhysicalSensor structure type should be allocated for each physical sensors (combo dev...
const registerwritelist_t MAG3110_Initialization[]
int8_t MAG3110_Init(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
registerreadlist_t MAG3110_DATA_READ[]
uint16_t isInitialized
Bitfields to indicate sensor is active (use SensorBitFields from build.h)
#define MAG3110_COUNTSPERUT
uint8_t iWhoAmI
sensor whoami
int8_t MAG3110_Idle(PhysicalSensor *sensor, SensorFusionGlobals *sfg)
SensorFusionGlobals sfg
This is the primary sensor fusion data structure.
uint16_t addr
I2C address if applicable.
#define F_USING_MAG
Definition: magnetic.h:38